<?php

namespace app\api\server\circle;

use app\api\model\circle\CircleApiModel;
use app\api\ServerBase;
use app\api\model\circle\CircleCommentApiModel;
use app\api\validate\circle\CircleCommentVal;
use app\logic\model\circle\CircleCommentLikeLogicModel;
use think\db\exception\DbException;
use think\facade\Db;
use think\Request;

class CircleCommentServer extends ServerBase
{
    function __construct( Request $request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new CircleCommentApiModel();
        $this->circleModel=new CircleApiModel();
        $this->likeModel=new CircleCommentLikeLogicModel();
        $this->validate = new CircleCommentVal();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $this->param['audit'] = 1;
        $this->param['status'] = 1;
        $field= 'id,user_id,content,apply_id,create_time';
        [$list,$count] = $this->model->getApiList($this->param,[],$field,$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }

    function getDetail(){
        $id =  $this->request->param('id');
        $info = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.select',1);
            return false;
        }
        $this->getMessageCode('adminMessage.success.select',2);
        return ['info'=>$info];
    }


    function add(){
        $post = $this->request->post();
        if(!$this->validate->scene('create')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $circle = $this->circleModel->where('id',$post['c_id'])
            ->where('audit',1)
            ->where('status',1)
            ->find();
        if (empty($circle)){
            $this->ERROR='帖子不存在';
            return false;
        }
        $post['user_id'] = $this->request->user_id;
        Db::startTrans();
        try {
            $this->model->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        if(!$this->validate->scene('update')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        Db::startTrans();
        try {
            $info->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function del(){
        $id = $this->request->param('id',0);
        if(empty($id)){
            $this->getMessageCode('adminMessage.error.delete',1);
            return false;
        }
        $info  = $this->model->find($id);
        if($info['user_id'] != $this->request->user_id){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        if($info && $info->delete()){
            $this->getMessageCode('adminMessage.success.delete',2);
            return true;
        }
        $this->getMessageCode('adminMessage.error.delete',1);
        return false;
    }


    function like_op(){
        $id = $this->param['id']??0;
        if(empty($id)){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        $info = $this->model->where('id',$id)->find();
        if(empty($info)){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        $like = $this->likeModel->where('user_id',$this->request->user_id)
            ->where('comment_id',$info['id'])->find();
        if(empty($like)){
            $this->likeModel->save([
                'user_id'=>$this->request->user_id,
                'comment_id'=>$info['id']
            ]);
        }else{
            $like->delete();
        }
        return true;
    }
}